/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.ItinerarioHorario;
import java.sql.ResultSet;
import java.util.ArrayList;
/**
 * @author Administrador
 */
public class AdminItinerarioHorario implements ItinerarioHorarioManager{
    private Connection c;
    public AdminItinerarioHorario(Connection c){
        this.c = c;
    }
    
    @Override
    public void saveItinerarioHorarios(List<ItinerarioHorario> horarios) throws DataException{
        PreparedStatement preSt = null;
        int i = 0;
        String query = "INSERT INTO ITINERARIO_HORARIO VALUES";
        try{
            for(ItinerarioHorario h:horarios){
                query += "("+h.getIdItinerarioGrupo()+","+h.getIdFacultad();
                query += ","+h.getIdPeriodoDia()+","+h.getIdDiaSemana();
                query += ","+h.getIdAula()+"),";
                i++;
            }
            if(i > 0){
                i = query.lastIndexOf(",");
                query = query.substring(0, i);
            }            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException e){
            throw new DataException(" AdminItinerarioHorario -> saveItinerarioHorarios(List<ItinerarioHorario> horarios) err sql = "+e.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    
    @Override
    public void eliminarPorItinerarioGrupo(int idItinerarioGrupo)throws DataException{
        PreparedStatement preSt = null;
        try{
            String query = "DELETE FROM ITINERARIO_HORARIO WHERE IDITINERARIO_GRUPO="+idItinerarioGrupo;
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException ex){
            throw new DataException(" AdminItinerarioHorario -> eliminarPorItinerarioGrupo(int idItinerarioGrupo) "+ex.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    
    public List<ItinerarioHorario> getItiHorariosPorGrupoBasic(int idItinerarioGrupo) throws DataException{
        List<ItinerarioHorario> ihorarios = new ArrayList<ItinerarioHorario>();
        ResultSet rs = null;
        PreparedStatement preSt = null;
        try{
            String query = "SELECT IDPERIODO_DIA";
            query += ", IDDIA_SEMANA";
            query += ", IDAULA";
            query += " FROM ITINERARIO_HORARIO";
            query += " WHERE IDITINERARIO_GRUPO="+idItinerarioGrupo;            
            preSt = c.prepareStatement(query);
            rs = preSt.executeQuery();
            while(rs.next()){
                ItinerarioHorario ih = new ItinerarioHorario();
                ih.setIdItinerarioGrupo(idItinerarioGrupo);
                ih.setIdPeriodoDia(rs.getInt("IDPERIODO_DIA"));
                ih.setIdDiaSemana(rs.getInt("IDDIA_SEMANA"));
                ih.setIdAula(rs.getInt("IDAULA"));
                ihorarios.add(ih);
            }
        }catch(SQLException e){
            throw new DataException(" AdminItinerarioHorario -> getItiHorariosPorGrupoBasic(int idItinerarioGrupo) sql = "+e.getMessage());
        }finally{
            SqlManager.closeRs(rs);
            SqlManager.closePreSt(preSt);
        }
        return ihorarios;
    }
}
